layui.use(['layer', 'form', 'jquery', 'table'], function () {
    var layer = layui.layer;
    var form = layui.form;
    var $ = layui.jquery;
    var table = layui.table;
    getTable();

    function getTable() {
        $.ajax({
            url: '/jinsi/getUserDevelopmentTable',
            type: 'post',
            dataType: 'json',
            success: function (data) {
                tablereload(data.data);
            }
        });
    }

    function tablereload(data) {
        table.render({
            elem: '#table',
            toolbar: true,
            data: data,
            async: true,
            title: '进思用户列表',
            page: {
                limit: 10,
                layout: ['count', 'prev', 'page', 'next', 'skip', 'refresh'],
                first: "首页",
                last: "尾页",
            },
            cols: [[
                {type: 'checkbox'},
                {field: 'userCode', title: '用户编号', align: 'center'},
                {
                    field: 'photoImg', title: '学生照片', align: 'center', templet: function (data) {
                        return "<img src='"+data.photoImg+"'/>";
                    }
                },
                {field: 'nickname', title: '学生名称', align: 'center'},
                {field: 'account', title: '学生账户', align: 'center'},
                {
                    field: 'studyDate', title: '学习时长', align: 'center', templet: function (data) {
                        return data.studyDate + " 分钟";
                    }
                },
                {field: 'profession', title: '所属专业', align: 'center'},
                {field: 'studyContent', title: '学习内容', align: 'center'},
                {
                    field: 'type', title: '状态', align: 'center', templet: function (data) {
                        if (data.type == 3) {
                            var radio = "<input type='checkbox' lay-skin='switch' name='type'disabled lay-skin='switch' lay-text='在读|已毕业'>";
                        } else {
                            var radio = "<input type='checkbox' lay-skin='switch' name='type' disabled lay-skin='switch' lay-text='在读|已毕业' checked>";
                        }
                        return radio;
                    }
                },
                {
                    field: 'status', title: '账号状态', align: 'center', templet: function (data) {
                        if (data.status == 2) {
                            var radio = "<input type='checkbox' lay-skin='switch' name='status' disabled lay-skin='switch' lay-text='正常|停用' checked>";
                        } else {
                            var radio = "<input type='checkbox' lay-skin='switch' name='status' disabled lay-skin='switch' lay-text='正常|停用'>";
                        }
                        return radio;
                    }
                },
                {field: 'consumption', title: '消费金额', align: 'center',templet:function (data) {
                        return data.consumption + " 元";
                    }},
                {field: 'integration', title: '量化积分', align: 'center',templet:function (data) {
                    return data.integration+" 分";
                    }},
                {
                    fiele: '', title: '操作', align: 'center', templet: function (data) {
                        return "<div><a href='javascript:getinformation(" + data.userId + ");' style='margin-right: 10px;color: #009688'>查看</a><a href='javascript:updateBeforeUser(\"" + data.userId + "\");' style='margin-right: 10px;color: #009688'>编辑</a></div>";
                    }
                }
            ]]
        });
    }

    //搜索
    form.on('submit(search)', function (data) {
        $.ajax({
            url: '/jinsi/getSearchUserDevelopment',
            type: 'post',
            dataType: 'json',
            data: data.field,
            success: function (data) {
                tablereload(data.data)
            }
        });
    });
    //查看
    getinformation = function getinformation(userId) {
        $.ajax({
            url: '/jinsi/getinformation',
            type: 'post',
            dataType: 'json',
            data: {'userId': userId},
            success: function (data) {
                devicesMsg(data.data);
            }
        });
    }

    function devicesMsg(data) {
        layer.open({
            type: 2,
            title: '进思用户信息',
            offset: 'auto',
            shade: 0,
            maxmin: true,
            area: ['1330px', '600px'],
            content: '/user/user-see-jinsi.html',
            btn: ['关闭'],
            success: function (layero, index) {
                var obj = layero.find('iframe').contents();
                obj.find('#avatarUrl').attr('src',data.avatarUrl);
                obj.find('#nickname').html(data.nickname);
                obj.find('input[name=nickname]').val(data.nickname);
                obj.find('input[name=fullName]').val(data.fullName);
                obj.find('input[name=userCode]').val(data.userCode);
                obj.find('input[name=account]').val(data.account);
                obj.find('input[name=profession]').val(data.profession);
                obj.find('input[name=studyDate]').val(data.studyDate);
                obj.find('input[name=integration]').val(data.integration);
                obj.find('input[name=consumption]').val(data.consumption);
                var type;
                if (data.type == 2) {
                    type = "在读"
                } else {
                    type = "已毕业";
                }
                obj.find('input[name=type]').val(type);
                obj.find('input[name=entranceDate]').val(data.entranceDate);
                obj.find('input[name=graduationDate]').val(data.graduationDate);
                obj.find('input[name=createDate]').val(data.createDate);
                var status;
                if (data.status == 2) {
                    status = "正常";
                } else if (data.status = 3) {
                    status = "禁言";
                } else if (data.status = 4) {
                    status = "注销";
                } else {
                    status = "封停";
                }
                obj.find('input[name=status]').val(status);
                obj.find('input[name=idCard]').val(data.idCard);
                obj.find('input[name=phone]').val(data.phone);
                obj.find('input[name=sex]').val(data.sex);
                obj.find('input[name=email]').val(data.email);
                obj.find('#studyContent').html(data.studyContent);
            }
        });
    }

    //编辑
    updateBeforeUser = function updateBeforeUser(userId) {
        $.ajax({
            url: '/jinsi/getinformation',
            type: 'post',
            dataType: 'json',
            data: {'userId': userId},
            success: function (data) {
                updateMsg(data.data);
            }
        });
    }

    function updateMsg(data) {
        layer.open({
            type: 2,
            title: '修改用户信息',
            offset: 'auto',
            shade: 0,
            maxmin: true,
            area: ['1650px', '630px'],
            content: '/user/user-jinsi-update.html',
            btn: ['修改', '取消'],
            success: function (layero, index) {
                var obj = layero.find('iframe').contents();
                obj.find('input[name=userId]').val(data.userId);
                obj.find('input[name=developmentId]').val(data.developmentId);
                obj.find('#avatarUrl').attr('src', data.avatarUrl);
                obj.find('#nickname').html(data.nickname);
                obj.find('input[name=avatarUrl]').val(data.avatarUrl);
                obj.find('input[name=nickname]').val(data.nickname);
                obj.find('input[name=userCode]').val(data.userCode);
                obj.find('input[name=fullName]').val(data.fullName);
                obj.find('input[name=profession]').val(data.profession);
                obj.find('input[name=studyDate]').val(data.studyDate);
                obj.find('input[name=integration]').val(data.integration);
                obj.find('input[name=consumption]').val(data.consumption);
                var type;
                if (data.type == 2) {
                    type = "在读"
                } else {
                    type = "已毕业";
                }
                selectLay(obj, data.type, type, '#type');
                obj.find('input[name=entranceDate]').val(data.entranceDate);
                obj.find('input[name=graduationDate]').val(data.graduationDate);
                obj.find('input[name=createDate]').val(data.createDate);
                var status;
                if (data.status == 2) {
                    status = "正常";
                } else if (data.status = 3) {
                    status = "禁言";
                } else if (data.status = 4) {
                    status = "注销";
                } else {
                    status = "封停";
                }

                obj.find('input[name=status]').val(status);
                obj.find('input[name=idCard]').val(data.idCard);
                obj.find('input[name=phone]').val(data.phone);
                obj.find('input[name=sex]').val(data.sex);
                obj.find('input[name=email]').val(data.email);
                obj.find('#studyContent').html(data.studyContent);
                selectLay(obj, data.sex, data.sex, '#sex');
            },
            yes: function (index, layero) {
                var obj = layero.find('iframe').contents();
                var param = obj.find('#form').serialize();
                $.ajax({
                    url: '/jinsi/updateUserDevelop',
                    type: 'post',
                    dataType: 'json',
                    data: param,
                    success: function (data) {
                        layer.msg("修改成功");
                        getTable();
                    }
                });
                layer.close(index);
            }
        });
    }
    //lay-value的下拉框矫正
    function selectLay(obj, layValue, value, select) {
        obj.find(select).children().each(function () {
            if ($(this).val() == layValue) {
                var status = 'dd[lay-value=' + layValue + ']';
                obj.find(select).siblings("div.layui-form-select").find('dl').find(status).click();
                $(this).parent().append('<option value="' + layValue + '" selected>' + value + '</option>');
            }
        })
    }
    //高级检索
    retrieval = function retrieval() {
        layer.open({
            type: 2,
            title: '高级检索',
            offset: 'auto',
            shade: 0,
            maxmin: true,
            area: ['400px', '340px'],
            content: '/user/user-advanced-search.html',
            btn: ['确定', '取消'],
            yes: function (index, layero) {
                var obj = layero.find('iframe').contents();
                var userCode = obj.find('input[name=userCode]').val();
                var consumption = obj.find('input[name=consumption]').val();
                var studyDate = obj.find('input[name=studyDate]').val();
                $.ajax({
                    url: '/jinsi/getUserDevelopmentTable',
                    type: 'post',
                    dataType: 'json',
                    data: {'userCode': userCode, 'consumption': consumption, 'studyDate': studyDate},
                    success: function (data) {
                        tablereload(data.data);
                    }
                });
                layer.close(index);
            }
        });
    }
    var userId;
    table.on('checkbox(test)', function(obj){
        userId = obj.data.userId;
    });
    giveCoupons = function giveCoupons(){
        var checkStatus = table.checkStatus('table').data;
        if (checkStatus.length<1){
            layer.msg("请选择用户",{icon:5});
        }else if (checkStatus.length == 1) {
            layer.open({
                type:2,
                title:'优惠券数据列表',
                shade:0,
                maxmin: true,
                offset:'auto',
                area:['1000px','650px'],
                content:"/user/user-give-coupons.html",
                success:function (layero,index) {
                    var obj = layero.find('iframe').contents();
                    obj.find('input[name=userId]').val(userId);
                }
            });
        }else {
            layer.msg("请选择一个用户",{icon:5});
        }
    }
});

//返回
function goBack() {
    //通过页面查找iframe
    parent.$('#recordLog').val($('#jinsiTable').text());
    parent.before();
    return false;
}
